package com.htc.android.footprints.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.InputFilter;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.ResourceCursorAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.htc.android.footprints.provider.FootprintManager;
import com.htc.android.footprints.utils.FootprintUtils;
import com.htc.android.footprints.utils.ImeLauncher;
import com.htc.android.footprints.utils.camera.FootprintImageManager;
import com.htc.android.footprints.vo.Category;
import com.htc.android.footprints.vo.Footprint;

public class SearchActivity extends Activity
  implements HtcAdapterView.OnItemClickListener, HtcAdapterView.OnItemLongClickListener
{
  private static final int CONTEXT_MENU = 3;
  private static final int DELETE_FOOTPRINT_DIALOG = 2;
  private static final int MENU_SORT = 1;
  private static final int MENU_VIEW = 0;
  private static final int SORT_DIALOG = 1;
  private static final int SORT_MODE_BY_DATE = 1;
  private static final int SORT_MODE_BY_NAME = 0;
  private static final String SORT_STRING_BY_DATE = "create_time desc";
  private static final String SORT_STRING_BY_NAME = "name asc";
  private static final String TAG = "SearchActivity";
  private static final int VIEW_DIALOG;
  protected ListModeAdapter mAdapter;
  private String mCacheTag;
  private int mCategoryIndex;
  private String mCategoryKeyword;
  private ArrayList<Category> mCategoryList;
  private String[] mCategoryNames;
  protected Cursor mCursor;
  protected TextView mEmptyText;
  protected View mEmptyView;
  protected Filter mFilter;
  protected FootprintManager mFootprintManager;
  protected HtcListView mListView;
  protected SearchBoxView mSearchBox;
  protected EditText mSearchEdit;
  private Footprint mSelectedFootprint;
  private int mSortMode;
  private String mSortString;

  private void init()
  {
    this.mListView = ((HtcListView)findViewById(16908298));
    this.mEmptyView = findViewById(16908292);
    this.mEmptyText = ((TextView)findViewById(2131492894));
    this.mSearchBox = ((SearchBoxView)findViewById(2131492893));
    this.mSearchEdit = this.mSearchBox.getTextField();
    this.mSearchEdit.setHint(getString(2131361878));
    this.mSearchEdit.setInputType(112);
    EditText localEditText = this.mSearchEdit;
    InputFilter[] arrayOfInputFilter = new InputFilter[1];
    arrayOfInputFilter[0] = new InputFilter.LengthFilter(20);
    localEditText.setFilters(arrayOfInputFilter);
    this.mSearchEdit.requestFocus();
    this.mSearchEdit.addTextChangedListener(new TextWatcher()
    {
      public void afterTextChanged(Editable paramEditable)
      {
        SearchActivity.this.updateEmptyText();
        SearchActivity.this.mFilter.filter(SearchActivity.this.mSearchEdit.getText());
      }

      public void beforeTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3)
      {
      }

      public void onTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3)
      {
      }
    });
    Intent localIntent = getIntent();
    this.mSortString = localIntent.getStringExtra("EXTRA_SORT_BY");
    if (this.mSortString != null)
      if (this.mSortString.equals("create_time desc"))
        this.mSortMode = 1;
    String str;
    ArrayList localArrayList;
    while (true)
    {
      this.mCategoryKeyword = localIntent.getStringExtra("EXTRA_CATEGORY");
      if (this.mCategoryKeyword == null)
        this.mCategoryKeyword = Category.getAllCategory().mKeyword;
      this.mCacheTag = localIntent.getStringExtra("EXTRA_CACHE_TAG");
      if (this.mCacheTag == null)
        this.mCacheTag = "ListModeActivity";
      str = localIntent.getStringExtra("EXTRA_SEARCH_INPUT");
      this.mFootprintManager = FootprintManager.getInstance(this);
      this.mCursor = this.mFootprintManager.findFootprintCursorByCategory(this.mCategoryKeyword, this.mSortString);
      this.mAdapter = new ListModeAdapter(this, 2130903062, this.mCursor);
      this.mAdapter.updateSections();
      this.mFilter = this.mAdapter.getFilter();
      new Handler().postDelayed(new Runnable()
      {
        public void run()
        {
          SearchActivity.this.mListView.setAdapter(SearchActivity.this.mAdapter);
          SearchActivity.this.switchView(SearchActivity.this.mCursor.getCount());
          SearchActivity.this.updateEmptyText();
        }
      }
      , 80L);
      this.mListView.setFastScrollEnabled(true);
      this.mListView.setOnItemClickListener(this);
      this.mListView.setOnItemLongClickListener(this);
      this.mListView.setRecyclerListener(this.mAdapter);
      localArrayList = FootprintManager.getInstance(this).getAllCategory();
      localArrayList.add(0, Category.getAllCategory());
      int i = localArrayList.size();
      this.mCategoryNames = new String[i];
      for (int j = 0; j < i; j++)
      {
        Category localCategory = (Category)localArrayList.get(j);
        this.mCategoryNames[j] = localCategory.mName;
        if (!localCategory.mKeyword.equals(this.mCategoryKeyword))
          continue;
        this.mCategoryIndex = j;
      }
      this.mSortMode = 0;
      continue;
      this.mSortMode = localIntent.getIntExtra("EXTRA_SORT_MODE", 1);
      updateSortString();
    }
    this.mCategoryList = localArrayList;
    if (!TextUtils.isEmpty(str))
    {
      this.mSearchEdit.setText(str);
      this.mSearchEdit.setSelection(str.length());
    }
  }

  private void switchView(int paramInt)
  {
    if (paramInt != 0)
    {
      this.mListView.setVisibility(0);
      this.mEmptyView.setVisibility(8);
    }
    while (true)
    {
      return;
      this.mListView.setVisibility(8);
      this.mEmptyView.setVisibility(0);
    }
  }

  private void updateEmptyText()
  {
    if (this.mSearchEdit.getText().length() > 0)
      this.mEmptyText.setText(2131361954);
    while (true)
    {
      return;
      if (this.mCategoryIndex != 0)
      {
        this.mEmptyText.setText(2131361953);
        continue;
      }
      this.mEmptyText.setText(2131361952);
    }
  }

  private void updateSortString()
  {
    if (this.mSortMode == 1);
    for (this.mSortString = "create_time desc"; ; this.mSortString = "name asc")
      return;
  }

  public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    requestWindowFeature(1);
    setContentView(2130903073);
    init();
  }

  protected Dialog onCreateDialog(int paramInt)
  {
    Object localObject = null;
    switch (paramInt)
    {
    default:
    case 1:
    case 0:
    case 2:
    case 3:
    }
    while (true)
    {
      if (localObject == null)
        localObject = super.onCreateDialog(paramInt);
      return localObject;
      localObject = new HtcAlertDialog.Builder(this).setTitle(2131361890).setIcon(2130837677).setNegativeButton(2131361796, null).setSingleChoiceItems(2131165185, this.mSortMode, new DialogInterface.OnClickListener()
      {
        public void onClick(DialogInterface paramDialogInterface, int paramInt)
        {
          if (paramInt != SearchActivity.this.mSortMode)
            switch (paramInt)
            {
            default:
            case 0:
            case 1:
            }
          while (true)
          {
            SearchActivity.this.updateSortString();
            SearchActivity.this.mFilter.filter(SearchActivity.this.mSearchEdit.getText());
            paramDialogInterface.dismiss();
            return;
            SearchActivity.access$002(SearchActivity.this, 0);
            continue;
            SearchActivity.access$002(SearchActivity.this, 1);
          }
        }
      }).create();
      continue;
      localObject = new HtcAlertDialog.Builder(this).setTitle(2131361830).setIcon(2130837677).setNegativeButton(2131361796, null).setSingleChoiceItems(this.mCategoryNames, this.mCategoryIndex, new DialogInterface.OnClickListener()
      {
        public void onClick(DialogInterface paramDialogInterface, int paramInt)
        {
          if (SearchActivity.this.mCategoryIndex != paramInt)
          {
            SearchActivity.access$302(SearchActivity.this, ((Category)SearchActivity.this.mCategoryList.get(paramInt)).mKeyword);
            SearchActivity.access$202(SearchActivity.this, paramInt);
            SearchActivity.this.updateEmptyText();
            SearchActivity.this.mFilter.filter(SearchActivity.this.mSearchEdit.getText());
          }
          paramDialogInterface.dismiss();
        }
      }).create();
      continue;
      if (this.mSelectedFootprint == null)
        continue;
      HtcAlertDialog.Builder localBuilder = new HtcAlertDialog.Builder(this).setTitle(2131361957).setIcon(17301543);
      String str = getString(2131361816);
      Object[] arrayOfObject = new Object[1];
      arrayOfObject[0] = this.mSelectedFootprint.mName;
      localObject = localBuilder.setMessage(String.format(str, arrayOfObject)).setPositiveButton(2131361893, new DialogInterface.OnClickListener()
      {
        public void onClick(DialogInterface paramDialogInterface, int paramInt)
        {
          paramDialogInterface.dismiss();
          FootprintUtils.deleteFootprint(SearchActivity.this.getApplicationContext(), SearchActivity.this.mSelectedFootprint);
          SearchActivity.this.mAdapter.updateSections();
        }
      }).setNegativeButton(2131361796, null).create();
      continue;
      if (this.mSelectedFootprint == null)
        continue;
      CharSequence[] arrayOfCharSequence = new CharSequence[4];
      arrayOfCharSequence[0] = getString(2131361807);
      arrayOfCharSequence[1] = getString(2131361808);
      arrayOfCharSequence[2] = getString(2131361825);
      arrayOfCharSequence[3] = getString(2131361972);
      localObject = new HtcAlertDialog.Builder(this).setTitle(this.mSelectedFootprint.mName).setIcon(2130837677).setItems(arrayOfCharSequence, new DialogInterface.OnClickListener()
      {
        public void onClick(DialogInterface paramDialogInterface, int paramInt)
        {
          switch (paramInt)
          {
          default:
          case 0:
          case 1:
          case 2:
          case 3:
          }
          while (true)
          {
            paramDialogInterface.dismiss();
            return;
            Intent localIntent = new Intent("com.htc.android.footprints.activity.FootprintDetailEditActivity");
            try
            {
              localIntent.putExtra("DATA", SearchActivity.this.mSelectedFootprint);
              SearchActivity.this.startActivity(localIntent);
            }
            catch (Exception localException)
            {
              Log.e("SearchActivity", "Disable to launch edit mode");
            }
            continue;
            SearchActivity.this.showDialog(2);
            continue;
            Footprint[] arrayOfFootprint = new Footprint[1];
            arrayOfFootprint[0] = SearchActivity.access$600(SearchActivity.this);
            FootprintUtils.exportKMZ(SearchActivity.this, SearchActivity.this.mSelectedFootprint.mName.replace(" ", "_"), arrayOfFootprint);
            continue;
            FootprintUtils.saveAsContacts(SearchActivity.this.getApplicationContext(), SearchActivity.this.mSelectedFootprint);
          }
        }
      }).create();
    }
  }

  public boolean onCreateOptionsMenu(Menu paramMenu)
  {
    paramMenu.add(0, 1, 1, getString(2131361829)).setIcon(2130837732);
    paramMenu.add(0, 0, 0, getString(2131361830)).setIcon(2130837733);
    return true;
  }

  public void onDestroy()
  {
    super.onDestroy();
    this.mSelectedFootprint = null;
    if (this.mCursor != null)
      this.mCursor.close();
    this.mCategoryList.clear();
    this.mCategoryList = null;
    this.mCategoryNames = null;
    this.mListView.setAdapter(null);
    this.mListView.setRecyclerListener(null);
    this.mFootprintManager = null;
  }

  public void onItemClick(HtcAdapterView<?> paramHtcAdapterView, View paramView, int paramInt, long paramLong)
  {
    Intent localIntent = new Intent("com.htc.android.footprints.activity.DetailViewActivity");
    localIntent.setFlags(67108864);
    localIntent.putExtra("DATA", this.mAdapter.getFootprint(paramInt));
    localIntent.putExtra("EXTRA_CATEGORY", this.mCategoryKeyword);
    localIntent.putExtra("EXTRA_SEARCH_INPUT", this.mSearchEdit.getText().toString());
    localIntent.putExtra("EXTRA_SORT_BY", this.mSortString);
    localIntent.putExtra("EXTRA_ITEM_IDX", paramInt);
    startActivity(localIntent);
    finish();
  }

  public boolean onItemLongClick(HtcAdapterView<?> paramHtcAdapterView, View paramView, int paramInt, long paramLong)
  {
    this.mSelectedFootprint = this.mAdapter.getFootprint(paramInt);
    showDialog(3);
    return true;
  }

  public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent)
  {
    if (paramInt == 84)
      finish();
    for (boolean bool = true; ; bool = super.onKeyDown(paramInt, paramKeyEvent))
      return bool;
  }

  public boolean onOptionsItemSelected(MenuItem paramMenuItem)
  {
    int i = paramMenuItem.getItemId();
    if (i == 1)
      showDialog(1);
    while (true)
    {
      return true;
      if (i != 0)
        continue;
      showDialog(0);
    }
  }

  public void onResume()
  {
    super.onResume();
    if (this.mSearchEdit.hasFocus())
      ImeLauncher.getInstance(this).show(this.mSearchEdit);
  }

  protected class ListModeAdapter extends ResourceCursorAdapter
    implements HtcAbsListView.RecyclerListener, SectionIndexer
  {
    private int columnIndexDate;
    private int columnIndexName;
    private int columnIndexTime;
    private boolean firstTime = true;
    FootprintImageManager imageManager = FootprintImageManager.getInstance();
    private String lastSectionString = "";
    private int positionIndex = 0;
    private Map<Integer, Integer> sectionIndex2PositionIndex = new HashMap();
    private ArrayList<String> sectionList = new ArrayList();
    SimpleDateFormat timeFormatter = new SimpleDateFormat("MM/dd/yy");

    ListModeAdapter(Context paramInt, int paramCursor, Cursor arg4)
    {
      super(paramCursor, localCursor);
      this.columnIndexTime = localCursor.getColumnIndexOrThrow("create_time");
      this.columnIndexName = localCursor.getColumnIndexOrThrow("name");
      this.columnIndexDate = localCursor.getColumnIndexOrThrow("short_date");
    }

    private void updateSections(Cursor paramCursor)
    {
      SearchActivity.this.mListView.setFastScrollSectionsDirty();
      this.lastSectionString = "";
      this.positionIndex = 0;
      this.sectionList.clear();
      this.sectionIndex2PositionIndex.clear();
      this.firstTime = true;
      if (paramCursor != null)
      {
        if (SearchActivity.this.mSortMode == 1)
          while (paramCursor.moveToPosition(this.positionIndex))
          {
            String str4 = paramCursor.getString(this.columnIndexDate);
            if (TextUtils.isEmpty(str4))
            {
              long l = paramCursor.getLong(this.columnIndexTime);
              str4 = this.timeFormatter.format(new Date(l));
            }
            if (!str4.equals(this.lastSectionString))
            {
              this.lastSectionString = str4;
              this.sectionIndex2PositionIndex.put(Integer.valueOf(this.sectionList.size()), Integer.valueOf(this.positionIndex));
              this.sectionList.add(str4);
            }
            this.positionIndex = (1 + this.positionIndex);
          }
        if (paramCursor.moveToPosition(this.positionIndex))
        {
          String str1 = paramCursor.getString(this.columnIndexName);
          if (!TextUtils.isEmpty(str1))
          {
            if (this.firstTime)
              break label301;
            if (!str1.startsWith(this.lastSectionString))
            {
              String str3 = str1.substring(0, 1);
              this.lastSectionString = str3;
              this.sectionIndex2PositionIndex.put(Integer.valueOf(this.sectionList.size()), Integer.valueOf(this.positionIndex));
              this.sectionList.add(str3);
            }
          }
          while (true)
          {
            this.positionIndex = (1 + this.positionIndex);
            break;
            label301: this.firstTime = false;
            String str2 = str1.substring(0, 1);
            this.lastSectionString = str2;
            this.sectionIndex2PositionIndex.put(Integer.valueOf(0), Integer.valueOf(0));
            this.sectionList.add(str2);
          }
        }
      }
    }

    public void bindView(View paramView, Context paramContext, Cursor paramCursor)
    {
      SearchActivity.ListModeItem localListModeItem = (SearchActivity.ListModeItem)paramView.getTag();
      Footprint localFootprint = new Footprint();
      FootprintManager.fillDataFromCursor(localFootprint, paramCursor);
      Drawable localDrawable;
      if ((localListModeItem.photo.getWidth() == 0) || (localListModeItem.photo.getHeight() == 0))
      {
        ViewGroup.LayoutParams localLayoutParams = localListModeItem.photo.getLayoutParams();
        localDrawable = this.imageManager.getScaledFastImage(SearchActivity.this.mCacheTag, localFootprint.mPicPath, localLayoutParams.width, localLayoutParams.height);
        localListModeItem.photo.setImageDrawable(localDrawable);
        if (TextUtils.isEmpty(localFootprint.mShortDate))
          break label383;
        localListModeItem.time.setText(localFootprint.mShortDate);
      }
      int i;
      String str1;
      SpannableString localSpannableString2;
      while (true)
      {
        Editable localEditable = SearchActivity.this.mSearchEdit.getText();
        i = localEditable.length();
        str1 = localEditable.toString().toLowerCase();
        if (localFootprint.mName == null)
          break label630;
        if (i <= 0)
          break label614;
        String str4 = localFootprint.mName.toLowerCase();
        localSpannableString2 = new SpannableString(localFootprint.mName);
        if (str4.startsWith(str1))
        {
          localSpannableString2.setSpan(new ForegroundColorSpan(-1), 0, i, 33);
          localSpannableString2.setSpan(new BackgroundColorSpan(-8408808), 0, i, 33);
        }
        int k = -1;
        String str5 = " " + str1;
        while (true)
        {
          k = str4.indexOf(str5, k + 1);
          if (k <= -1)
            break;
          localSpannableString2.setSpan(new ForegroundColorSpan(-1), k + 1, 1 + (i + k), 33);
          localSpannableString2.setSpan(new BackgroundColorSpan(-8408808), k + 1, 1 + (i + k), 33);
        }
        localDrawable = this.imageManager.getScaledFastImage(SearchActivity.this.mCacheTag, localFootprint.mPicPath, localListModeItem.photo.getWidth(), localListModeItem.photo.getHeight());
        break;
        label383: localListModeItem.time.setText(this.timeFormatter.format(new Date(localFootprint.mCreateTime)));
      }
      localListModeItem.name.setText(localSpannableString2);
      while (true)
      {
        if (localFootprint.mAddr == null)
          break label670;
        if (i <= 0)
          break;
        String str2 = localFootprint.mAddr.toLowerCase();
        SpannableString localSpannableString1 = new SpannableString(localFootprint.mAddr);
        if (str2.startsWith(str1))
        {
          localSpannableString1.setSpan(new ForegroundColorSpan(-1), 0, i, 33);
          localSpannableString1.setSpan(new BackgroundColorSpan(-8408808), 0, i, 33);
        }
        int j = -1;
        String str3 = " " + str1;
        while (true)
        {
          j = str2.indexOf(str3, j + 1);
          if (j > -1)
          {
            localSpannableString1.setSpan(new ForegroundColorSpan(-1), j + 1, 1 + (i + j), 33);
            localSpannableString1.setSpan(new BackgroundColorSpan(-8408808), j + 1, 1 + (i + j), 33);
            continue;
            label614: localListModeItem.name.setText(localFootprint.mName);
            break;
            label630: localListModeItem.name.setText("");
            break;
          }
        }
        localListModeItem.address.setText(localSpannableString1);
      }
      while (true)
      {
        return;
        localListModeItem.address.setText(localFootprint.mAddr);
        continue;
        label670: localListModeItem.address.setText("");
      }
    }

    public void changeCursor(Cursor paramCursor)
    {
      SearchActivity.this.switchView(paramCursor.getCount());
      updateSections(paramCursor);
      super.changeCursor(paramCursor);
    }

    public Footprint getFootprint(int paramInt)
    {
      Cursor localCursor = (Cursor)getItem(paramInt);
      Footprint localFootprint1;
      if (localCursor != null)
      {
        localFootprint1 = new Footprint();
        FootprintManager.fillDataFromCursor(localFootprint1, localCursor);
      }
      for (Footprint localFootprint2 = localFootprint1; ; localFootprint2 = null)
        return localFootprint2;
    }

    public Footprint[] getFootprints()
    {
      int i = getCount();
      Footprint[] arrayOfFootprint = new Footprint[i];
      for (int j = 0; j < i; j++)
        arrayOfFootprint[j] = getFootprint(j);
      return arrayOfFootprint;
    }

    public int getPositionForSection(int paramInt)
    {
      try
      {
        int j = ((Integer)this.sectionIndex2PositionIndex.get(Integer.valueOf(paramInt))).intValue();
        i = j;
        return i;
      }
      catch (Exception localException)
      {
        while (true)
        {
          Log.e("SearchActivity", "getPositionForSection() error: " + paramInt);
          int i = 0;
        }
      }
    }

    public int getSectionForPosition(int paramInt)
    {
      return 0;
    }

    public Object[] getSections()
    {
      Object localObject;
      if (this.sectionList.size() > 0)
        localObject = this.sectionList.toArray();
      while (true)
      {
        return localObject;
        localObject = new String[1];
        localObject[0] = " ";
      }
    }

    public View newView(Context paramContext, Cursor paramCursor, ViewGroup paramViewGroup)
    {
      View localView = super.newView(paramContext, paramCursor, paramViewGroup);
      SearchActivity.ListModeItem localListModeItem = new SearchActivity.ListModeItem();
      localListModeItem.photo = ((ImageView)localView.findViewById(2131492899));
      localListModeItem.name = ((TextView)localView.findViewById(2131492900));
      localListModeItem.address = ((TextView)localView.findViewById(2131492901));
      localListModeItem.time = ((TextView)localView.findViewById(2131492902));
      localView.setTag(localListModeItem);
      return localView;
    }

    public void onMovedToScrapHeap(View paramView)
    {
      ((SearchActivity.ListModeItem)paramView.getTag()).photo.setImageDrawable(null);
    }

    public Cursor runQueryOnBackgroundThread(CharSequence paramCharSequence)
    {
      if (paramCharSequence == null)
        paramCharSequence = "";
      return SearchActivity.this.mFootprintManager.getCursorSortBy(SearchActivity.this.mCategoryKeyword, paramCharSequence.toString(), SearchActivity.this.mSortString);
    }

    public void updateSections()
    {
      new Handler().postDelayed(new Runnable()
      {
        public void run()
        {
          SearchActivity.ListModeAdapter.this.updateSections(SearchActivity.this.mAdapter.getCursor());
        }
      }
      , 300L);
    }
  }

  protected static class ListModeItem
    implements HtcListItemSeparable
  {
    TextView address;
    TextView name;
    ImageView photo;
    TextView time;

    public boolean shouldDrawOnThis()
    {
      return true;
    }

    public boolean shouldSeparate(Object paramObject)
    {
      return false;
    }
  }
}

/* Location:           E:\workspace\dex2java\dex-translator-0.0.9.3\HtcFootprints_dex2jar.jar
 * Qualified Name:     com.htc.android.footprints.activity.SearchActivity
 * JD-Core Version:    0.6.0
 */